package org.fpp.jdk21study;

import jdk.internal.vm.Continuation;
import jdk.internal.vm.ContinuationScope;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;

/**
 * @author bigbird-0101
 * @date 2024-02-20 14:57
 */
public class VirtualThreadsStudy {
    public static void main(String[] args) {
        try (var executor = Executors.newThreadPerTaskExecutor(Thread.ofVirtual().name("虚拟线程名",1).factory())) {
            IntStream.range(0, 10_000).forEach(i -> {
                executor.submit(() -> {
                    Thread thread = Thread.currentThread();
                    System.out.println(thread.getName()+":::");
                    Thread.sleep(Duration.ofSeconds(1));
                    return i;
                });
            });
        }
    }
    static class TestB{
        public static void main(String[] args) {
            List<Thread> list=new ArrayList<>();
            for(int i = 0; i < 9; i++) {
                Thread thread = Thread.startVirtualThread(() -> {
                    synchronized (TestB.class) {
                        System.out.println(Thread.currentThread().getName() + "::" + 123123);
                        try {
                            Thread.sleep(Duration.ofSeconds(15));
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
                list.add(thread);
            }
            Thread.startVirtualThread(() -> {
                System.out.println(Thread.currentThread().getName()+"-----dddd------");
                System.out.println("ddddddd");
            });
            list.forEach(s-> {
                try {
                    s.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });

        }
    }


    static class TestCD{
        private static final Lock lock = new ReentrantLock();
        public static void main(String[] args) {
            List<Thread> list=new ArrayList<>();
            for(int i = 0; i < 9; i++) {
                Thread thread = Thread.startVirtualThread(() -> {
                    lock.lock();
                    try{
                        System.out.println(Thread.currentThread().getName() + "::" + 123123);
                        try {
                            Thread.sleep(Duration.ofSeconds(15));
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }finally {
                        lock.unlock();
                    }
                });
                list.add(thread);
            }
            Thread.startVirtualThread(() -> {
                System.out.println(Thread.currentThread().getName()+"-----dddd------");
                System.out.println("ddddddd");
            });
            list.forEach(s-> {
                try {
                    s.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });

        }
    }

    static class TestC{
        public static void main(String[] args) {
            ContinuationScope scope = new ContinuationScope("scope");
            Continuation continuation = new Continuation(scope, () -> {
                System.out.println("before yield开始");
                Continuation.yield(scope);
                System.out.println("after yield 结束");
            });
            System.out.println("1 run");
            // 第一次执行Continuation.run
            continuation.run();
            System.out.println("2 run");
            // 第二次执行Continuation.run
            continuation.run();
            System.out.println("Done");
        }
    }
}
